const conn = require("../../libs/DB/Conn");
const _ = require("underscore");
const {createSql, projectDeviceDataUniq,todayTimeRange} = require("./dataAnalysisUtil");

module.exports = async function(params){
    let time = todayTimeRange();
    let sql = createSql(params.type,params.userDomainId,"day",time.range,{type:"project"},null,null);

    let result = await conn(sql.sql);
    result = projectDeviceDataUniq(result);
    let projectGroup = _.groupBy(result,"projectId");
    let chartObj = {
        xAxis:[],
        data:[{
            name:"今日",
            data:[],
        },{
            name:"去年今日",
            data:[],
        },{
            name:"昨日",
            data:[],
        }]
    }
    for(const projectId in projectGroup){
        let projectData = projectGroup[projectId];
        let dataTypeGroup = _.groupBy(projectData,"dataTime");
        chartObj.xAxis.push(projectData[0].projectName);
        chartObj.data[0].data.push(dataTypeGroup[time.queryTime[0]] ? dataTypeGroup[time.queryTime[0]][0].consumption : "-");
        chartObj.data[1].data.push(dataTypeGroup[time.queryTime[1]] ? dataTypeGroup[time.queryTime[1]][0].consumption : "-");
        chartObj.data[2].data.push(dataTypeGroup[time.queryTime[2]] ? dataTypeGroup[time.queryTime[2]][0].consumption : "-");
    }

    return {success:true,chartObj:chartObj};

}